package com.itheima.config;/*
 *      .--,       .--,
 *     ( (  \.---./  ) )
 *     '.__/o   o\__.'
 *        {=  ^  =}
 *         >  -  <
 *        /       \
 *       //       \\
 *      //|   .   |\\
 *      "'\       /'"_.-~^`'-.
 *         \  _  /--'         `
 *       ___)( )(___
 *      (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之。
 *
 * @author JiaoDaoNiu
 * @create 2022-09-21-20:27
 */

import com.itheima.common.JacksonObjectMapper;
import com.itheima.filter.LoginCheckInterceptor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    //注入
    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/employee/login")
                .excludePathPatterns("/employee/logout")
                .excludePathPatterns("/front/**")
                .excludePathPatterns("/backend/**")
                .excludePathPatterns("/common/**")
                .excludePathPatterns("user/sendMsd")
                .excludePathPatterns("user/login")
                .excludePathPatterns("/aliapy")
                .excludePathPatterns("/user/sendMsg")
                .excludePathPatterns("/user/login");
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行静态资源映射...");

        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");

    }


    /**
     * 扩展mvc框架的消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器");
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0,messageConverter);
    }


}
